package com.manman.eduService.controller;


import com.manman.commonUtils.Result;
import com.manman.eduService.client.VodClient;
import com.manman.eduService.entity.EduVideo;
import com.manman.eduService.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class EduVideoController {

    // 注入 vodClient
    @Autowired
    VodClient vodClient;

    @Autowired
    private EduVideoService videoService;

    // 添加小节
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return Result.ok();
    }

    // 删除小节
    @DeleteMapping("{id}")
    public Result deleteVideo(@PathVariable String id) {
        // 根据小节 id 获取视频 id，再删除
        EduVideo eduVideo = videoService.getById(id);
        String sourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(sourceId)){
            Result result = vodClient.removeAlyVideo(sourceId);
        }
        // 删除小节
        videoService.removeById(id);
        return Result.ok();
    }

    // 根据章节 ID 查询小节
    @GetMapping("getVideoById/{videoId}")
    public Result getVideoById(@PathVariable String videoId) {
        EduVideo video = videoService.getById(videoId);
        return Result.ok().data("video", video);
    }

    // 修改小节
    @PostMapping("updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return Result.ok();
    }
}

